<?php
class Auth extends Controller
{
	function Auth ()
	{
		parent::Controller ();
		$this->load->library('Form_validation');
		$this->rain->authorize (TRUE);
	}
	
	function index ()
	{
		$this->login ();
	}
	
	function login ()
	{
		$form =& $this->form_validation;
		$auth =& $this->dx_auth;
		
		$form->set_rules ('login', 'lang:RAIN_auth_email', 'required|trim|xss_clean');
		$form->set_rules ('password', 'lang:RAIN_auth_password', 'required|trim|xss_clean');
		$form->set_rules ('remember', 'lang:RAIN_auth_remember', 'integer');
		$form->set_error_delimiters ('<div class="login_message message error"><p>', '</p></div>');
		
		if ($auth->is_logged_in ())
		{
			redirect ('/');
			return;
		}
		$this->load->view ('default/head');
		if ( ! $form->run ())
		{
			$this->load->view ('default/login');
			return;
		}
		
		if ($auth->login ($this->input->post ('login'), $this->input->post ('password'), $this->input->post ('remember')))
		{
			redirect ('/');
		}
		else
		{
			if ($auth->is_banned ())
			{
				$error_str = sprintf ($this->lang->line ('RAIN_auth_banned'), $auth->get_ban_reason ());
			}
			else
			{
				$error_str = $this->lang->line ('RAIN_auth_invalid_login');
			}
			$this->load->view ('default/login', array ('error' => $error_str));
		}
	}
	
	function logout ()
	{
		$auth =& $this->dx_auth;
		$auth->logout ();
		
		redirect ('/auth/login');
	}
	
	function reclaim ()
	{
		$form =& $this->form_validation;
		$auth =& $this->dx_auth;
		
		$form->set_rules ('login', 'Email', 'required|trim|xss_clean');
		if ( ! $form->run ())
		{
			/*
			 * Form Failed, a field is invalid.
			 * Display the form again with the given data.
			 * KILL THE SCRIPT
			 */
		}
		if ($auth->is_logged_in ())
		{
			/*
			 * User is already logged in.
			 * Display error page with notification.
			 * KILL THE SCRIPT
			 */
		}
		if ($auth->forgot_password ($this->input->post ('login')))
		{
			/*
			 * Email sent to user's account.
			 */
		}
		else
		{
			/*
			 * Error with finding password.
			 * To display error:
			 * $auth->get_auth_error ()
			 */
		}
	}
}